package com.rzjm.exercise.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.*;
import com.rzjm.exercise.module.service.*;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping(value = "/module/eventsRecord",produces ="application/json;charset=UTF-8")
public class EventsRecordController extends BaseController {
    @Resource
    private EventsRecordService eventsRecordService;
    @Resource
    private EventsRecordImgService eventsRecordImgService;
    @Resource
    private TownsService townsService;
    @Resource
    private ShopService shopService;

    @Value("${projecturl.imgurl}")
    private String imgurl;

    @Log("查询举报记录页面")
    @GetMapping("")
    @RequiresPermissions("module:eventsRecord:eventsRecord")
    String eventsRecord(Model model) {
        UserDO user = getUser();
        QueryWrapper<Towns> qwTown = new QueryWrapper<>();
        qwTown.eq("parent_id", 0);
        qwTown.eq("district_id", 1319);
        List<Towns> townsList = townsService.list(qwTown);
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 1);
        wrapper.ne("shop_type", 3);
        if (user.getTownId() != null && user.getTownId() > 0) {
            wrapper.eq("town_id", user.getTownId());
        }
        List<Shop> shopList = shopService.list(wrapper);
        model.addAttribute("townsList", townsList);
        model.addAttribute("shopList", shopList);
        model.addAttribute("user", user);
        return "module/eventsRecord/list";
    }


    @Log("查询举报记录页面")
    @ResponseBody
    @GetMapping("/list")
    public DataTableLayUI<EventsRecord> list(DataTableLayUI<EventsRecord> condition) {
        return eventsRecordService.getListPage(condition);
    }

    /**
     * 删除
     */
    @Log("删除举报记录")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("module:eventsRecord:remove")
    public R remove( Integer id) {
        if (eventsRecordService.removeById(id) ) {
            return R.ok();
        }
        return R.error();
    }

    @Log("举报记录详情页面")
    @GetMapping("/showDetail/{id}")
    @RequiresPermissions("module:eventsRecord:edit")
    String showDetail(@PathVariable("id") Integer id, Model model) {
        EventsRecord eventsRecord = eventsRecordService.findDetailById(id);
        QueryWrapper<EventsRecordImg> wrapper = new QueryWrapper<>();
        wrapper.eq("record_id",eventsRecord.getId());
        wrapper.eq("img_type",0);
        List<EventsRecordImg> imgList = eventsRecordImgService.list(wrapper);
        wrapper.clear();
        wrapper.eq("record_id",eventsRecord.getId());
        wrapper.eq("img_type",1);
        List<EventsRecordImg> handleImgList = eventsRecordImgService.list(wrapper);
        for (int i = 0; i < imgList.size(); i++) {
            EventsRecordImg recordImg = imgList.get(i);
            recordImg.setImgUrl(imgurl + recordImg.getImgUrl());
        }
        for (int i = 0; i < handleImgList.size(); i++) {
            EventsRecordImg recordImg = handleImgList.get(i);
            recordImg.setImgUrl(imgurl + recordImg.getImgUrl());
        }
        eventsRecord.setCreateTimeStr(DateUtils.getTime(eventsRecord.getCreateTime()));
        eventsRecord.setHandleDeptTimeStr(DateUtils.getTime(eventsRecord.getHandleDeptTime()));
        eventsRecord.setSafeSubmitTimeStr(DateUtils.getTime(eventsRecord.getSafeSubmitTime()));
        eventsRecord.setSafeHandleTimeStr(DateUtils.getTime(eventsRecord.getSafeHandleTime()));
        model.addAttribute("eventsRecord", eventsRecord);
        model.addAttribute("imgList", imgList);
        model.addAttribute("handleImgList", handleImgList);
        return "module/eventsRecord/detail";
    }
}
